package xyz.ddlnt.serviceusr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Service;
import xyz.ddlnt.commonutil.constant.MqConstants;
import xyz.ddlnt.commonutil.util.RandomUtil;
import xyz.ddlnt.model.dto.FollowDTO;
import xyz.ddlnt.model.dto.PageDTO;
import xyz.ddlnt.model.entity.Follow;
import xyz.ddlnt.model.vo.FollowVO;
import xyz.ddlnt.serviceusr.mapper.FollowMapper;
import xyz.ddlnt.serviceusr.service.FollowService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author ZZULI_SE 210910
 * @data 2025/5/10 21:30
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements FollowService {
    @Resource
    private FollowMapper followMapper;
    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public Boolean FollowUser(FollowDTO followDTO) {
        Follow follow = Follow.builder()
                .id(RandomUtil.generate12DigitRandomNumber())
                .followerId(followDTO.getFollowerId())
                .followedId(followDTO.getFollowedId())
                .followTime(LocalDateTime.now())
                .build();
        if (followMapper.insert(follow) == 1) {
            // 关注；被关注用户粉丝加一
            rabbitTemplate.convertAndSend(MqConstants.USR_USER, MqConstants.FANS_PLUS_ONE_QUEUE, followDTO.getFollowedId());
            // 关注：用户关注数量加一
            rabbitTemplate.convertAndSend(MqConstants.USR_USER, MqConstants.FOLLOW_PLUS_ONE_QUEUE, followDTO.getFollowerId());
            return true;
        }
        return false;
    }

    @Override
    public Boolean unfollowUser(FollowDTO followDTO) {
        LambdaQueryWrapper<Follow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper
                .eq(Follow::getFollowerId, followDTO.getFollowerId())
                .eq(Follow::getFollowedId, followDTO.getFollowedId())
        );
        if (followMapper.delete(queryWrapper) == 1) {
            // 取关：被关注用户粉丝减一
            rabbitTemplate.convertAndSend(MqConstants.USR_USER, MqConstants.FANS_MINUS_ONE_QUEUE, followDTO.getFollowedId());
            // 关注：用户关注数量减一
            rabbitTemplate.convertAndSend(MqConstants.USR_USER, MqConstants.FOLLOW_MINUS_ONE_QUEUE, followDTO.getFollowerId());
            return true;
        }
        return false;
    }

    @Override
    public Boolean isFollowed(FollowDTO followDTO) {
        return followMapper.isExits(followDTO);
    }

    @Override
    public List<FollowVO> getFollowList(PageDTO pageDTO) {
        IPage<FollowVO> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        return followMapper.getFollowList(page,  pageDTO.getId());
    }

    @Override
    public List<FollowVO> getFansList(PageDTO pageDTO) {
        IPage<FollowVO> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        return followMapper.getFansList(page,  pageDTO.getId());
    }
}
